package com.cwx.mychat.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwx.mychat.entity.dto.TokenUserInfoDto;
import com.cwx.mychat.entity.enums.GroupAdminTypeEnum;
import com.cwx.mychat.entity.enums.UserContactTypeEnum;
import com.cwx.mychat.entity.po.ChatSession;
import com.cwx.mychat.entity.po.ChatSessionUser;
import com.cwx.mychat.entity.po.GroupInfo;
import com.cwx.mychat.entity.po.UserInfo;
import com.cwx.mychat.mappers.ChatSessionMapper;
import com.cwx.mychat.mappers.ChatSessionUserMapper;
import com.cwx.mychat.service.ChatSessionService;
import com.cwx.mychat.service.ChatSessionUserService;
import com.cwx.mychat.utils.common.CommonUtil;
import com.cwx.mychat.utils.stringUtils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;

/**
 * 作者： cwx
 * 时间： 2024/9/21 14:33
 * 版本： 1.0
 * 注释：
 */
@Slf4j
@Service
public class ChatSessionUserServiceImpl extends ServiceImpl<ChatSessionUserMapper, ChatSessionUser> implements ChatSessionUserService {


    @Autowired
    private ChatSessionUserMapper chatSessionUserMapper;


    @Autowired
    private ChatSessionService chatSessionService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED) //事务传播,如果当前没有事务，则创建一个新的事务；如果已经存在事务，则加入该事务。
    public int addSession(TokenUserInfoDto tokenUserInfoDto, Serializable serializable, String helloMsg, Integer contactType) {
        UserInfo userInfo = null;
        GroupInfo groupInfo = null;
        if(contactType.equals(UserContactTypeEnum.USER.getType())){
            userInfo = (UserInfo)serializable;
        }else{
            groupInfo = (GroupInfo)serializable;
        }
        if(userInfo != null){
            log.info("开始添加用户会话");
            ChatSessionUser chatSessionUser = new ChatSessionUser();
            chatSessionUser.setSessionId(CommonUtil.getChatSessionIdUser(new String[]{tokenUserInfoDto.getUserId(), userInfo.getUserId()}));
            chatSessionUser.setUserId(tokenUserInfoDto.getUserId());
            chatSessionUser.setContactId(userInfo.getUserId());
            chatSessionUser.setNickName(tokenUserInfoDto.getNickName());
            chatSessionUser.setContactName(userInfo.getNickName());
            //插入或更新会话
            int i = chatSessionUserMapper.insertOrUpdateSessionUser(chatSessionUser);
            if(i == 0){
                log.info("插入或更新会话没做任何操作");
            }else if(i == 1){
                log.info("插入会话成功, 没有发生更新");
            }else if(i == 2){
                log.info("更新会话成功");
            }
            log.info("开始添加会话最终语句");
            boolean b1 = chatSessionService.insertOrUpdateSession(chatSessionUser);
            if(b1) return i;
            else return 0;

        }
        if(groupInfo != null){
            log.info("开始添加群会话");
            ChatSessionUser chatSessionUser = new ChatSessionUser();
            chatSessionUser.setSessionId(CommonUtil.getChatSessionIdUser(new String[]{tokenUserInfoDto.getUserId(), groupInfo.getGroupId()}));
            chatSessionUser.setUserId(tokenUserInfoDto.getUserId());
            chatSessionUser.setContactId(groupInfo.getGroupId());
            chatSessionUser.setNickName(tokenUserInfoDto.getNickName());
            chatSessionUser.setContactName(groupInfo.getGroupName());
            chatSessionUser.setIsAdmin(GroupAdminTypeEnum.NOT_ADMIN.getStatus());
            //插入或者更新会话
            int i = chatSessionUserMapper.insertOrUpdateSessionUser(chatSessionUser);
            if(i == 0){
                log.info("插入或更新会话没做任何操作");
            }else if(i == 1){
                log.info("插入会话成功, 没有发生更新");
            }else if(i == 2){
                log.info("更新会话成功");
            }
            log.info("开始添加会话最终语句");
            boolean b1 = chatSessionService.insertOrUpdateSession(chatSessionUser, helloMsg);
            if(b1) return i;
            else return 0;
        }
        return 0;
    }
}
